/*
  LiquidCrystal Library - Hello World
 
 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the 
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.
 
 This sketch prints "Hello World!" to the LCD
 and shows the time.
 
  The circuit:
 * LCD RS pin to digital pin 26
 * LCD Enable pin to digital pin 27
 * LCD D4 pin to digital pin 22
 * LCD D5 pin to digital pin 23
 * LCD D6 pin to digital pin 24
 * LCD D7 pin to digital pin 25
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe
 modified for distance on Jbots 18 Jul 2012
 by Jonathan Price
 
 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

// include the library code:
#include <LiquidCrystal.h>

int pingPin = A0; // Ping sensor is connected to port A0

long duration, inches, cm; // Define variables for Ping sensor 

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd1(26, 27, 22, 23, 24, 25);

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd1.begin(16, 2);
  // Print a message to the LCD.
  lcd1.print("Distance:");

}

void loop() {
  
  delay(100);
  
ping();  
lcd1.setCursor(0,1);//Set the position of the cursor [line, space]
lcd1.print("            ");//If a 2-digit number is printed, then a 1-digit number, the second digit will remain. This space covers the second digit.
  lcd1.setCursor(0, 1);
lcd1.print(inches);//This prints the variable inches to the screen.
lcd1.print(" ");//On some LCDs, whenever a certain number is displayed, it goes dim. Having a space at the end prevents this.
}

//This section operates the PING sensor.
unsigned long ping() {
pinMode(pingPin, OUTPUT); // Make the Pingpin to output
digitalWrite(pingPin, LOW); //Send a low pulse
delayMicroseconds(2); // wait for two microseconds
digitalWrite(pingPin, HIGH); // Send a high pulse
delayMicroseconds(5); // wait for 5 micro seconds
digitalWrite(pingPin, LOW); // send a low pulse
pinMode(pingPin,INPUT); // switch the Pingpin to input
duration = pulseIn(pingPin, HIGH); //listen for echo

/*Convert micro seconds to Inches
-------------------------------------*/

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
}

long microsecondsToInches(long microseconds) // converts time to a distance
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) // converts time to a distance
{
return microseconds / 29 / 2;
}

